package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.pickerview.TimePickerView;
import com.blankj.utilcode.util.TimeUtils;
import com.stark.cartoonavatarmaker.lib.core.AvatarPartType;
import com.stark.cartoonavatarmaker.lib.core.AvatarRole;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flc.ast.R;
import flc.ast.activity.DressUpActivity;
import flc.ast.databinding.FragmentInfoBinding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link InfoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class InfoFragment extends BaseNoModelFragment<FragmentInfoBinding> {

    private AvatarRole avatarRole;
    private TimePickerView pvTime;

    public static InfoFragment newInstance(@NonNull AvatarRole role) {
        InfoFragment fragment = new InfoFragment();
        fragment.avatarRole = role;
        return fragment;
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_info;
    }

    @Override
    protected void initView() {
        if (avatarRole != null) {
            mDataBinding.etName.setText(avatarRole.name);
            mDataBinding.etDesc.setText(avatarRole.desc);
            mDataBinding.tvBirth.setText(TimeUtils.millis2String(avatarRole.dateOfBirth, new SimpleDateFormat("yyyy年MM月dd日")));
        } else {
            mDataBinding.tvBirth.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy年MM月dd日")));
        }
        showTimePicker();

        mDataBinding.ivBirth.setOnClickListener(this);

        mDataBinding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                ((DressUpActivity) getActivity()).setName(str);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mDataBinding.etDesc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                ((DressUpActivity) getActivity()).setDesc(str);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivBirth:
                pvTime.show();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //显示时间
    public void showTimePicker() {
        pvTime = new TimePickerView.Builder(getActivity(), new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
                String birthday = format.format(date);
                mDataBinding.tvBirth.setText(birthday);
                ((DressUpActivity) getActivity()).setBirth(date.getTime());
            }
        }).setSubmitText(getString(R.string.confirm))
                .setCancelText(getString(R.string.cancel))
                .setCancelColor(Color.BLACK)
                .setSubmitColor(Color.BLACK)
                .setSubCalSize(16)
                //.isDialog(true) //是否对话框样式显示（显示在页面中间）
                //.isCyclic(true) //是否循环滚动
                .setType(new boolean[]{true, true, true, false, false, false}) //显示“年月日时分秒”的哪几项
                .isCenterLabel(false) //是否只显示选中的label文字，false则每项item全部都带有 label
                .build();
        //设置显示的日期
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy年MM月dd日").parse(mDataBinding.tvBirth.getText().toString()));
        } catch (ParseException e) {
            //e.printStackTrace();
        }
        //这里需要注意的是月份是从0开始的，要显示10月份这里的参数应该是9
        //calendar.set(1997,9,10);
        pvTime.setDate(calendar);
    }

}